package cn.tedu.submarine;

import sun.security.provider.PolicySpiFile;

import javax.swing.ImageIcon;
import java.awt.*;
import java.util.Random;

public abstract class SeaObject {
    public static final int LIVE = 1;
    public static final int DEAD = 0;
    protected int state = LIVE;
    //public int state;

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x=-width;
        Random rand=new Random();
        y = rand.nextInt(World.HEIGHT-height-150+1)+150;
        speed = rand.nextInt(3)+1;
    }

    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract ImageIcon getImage();

    public abstract void move();

    public boolean isLive(){
        return state==LIVE;
    }

    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if(this.isLive()){
         this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    public boolean outOfBounds(){
        return this.x>=World.WIDTH;
    }

    public boolean isHit(SeaObject other){
        int x1 =this.x - other.width;
        int x2 = this.x +this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+ this.height;
        int x = other.x;
        int y = other.y;
        return (x>=x1 && x<=x2 && y>y1 && y<=y2);
    }
    public void goDead(){
        state = DEAD;
    }
}
